package basicmorse;
import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;

import static basicmorse.MorseDecoder.Token.*;

public class MorseQueueTest {
	MorseQueue aQueue;

	@Before
	public void setUp() throws Exception {
		aQueue = new MorseQueue();
	}
	
	@Test
	public void oneTokenShortPause() {
		aQueue.addToken(DOT);
		aQueue.addToken(SHORT_PAUSE);
		
		assertTrue(aQueue.hasNext() == true);		
		String readable = aQueue.getNextDecoded();
		assertTrue(readable.equals("e"));

		assertTrue(aQueue.hasNext() == false);
	}
	
	@Test
	public void twoTokensShortPause() {
		aQueue.addToken(DASH);
		aQueue.addToken(DOT);
		aQueue.addToken(SHORT_PAUSE);
		
		assertTrue(aQueue.hasNext() == true);
		
		String readable = aQueue.getNextDecoded();
		assertTrue(readable.equals("n"));
		
		assertTrue(aQueue.hasNext() == false);
	}
	
	@Test
	public void threeTokensShortPause() {
		aQueue.addToken(DOT);
		aQueue.addToken(DOT);
		aQueue.addToken(DOT);
		aQueue.addToken(SHORT_PAUSE);
		
		assertTrue(aQueue.hasNext() == true);
		
		String readable = aQueue.getNextDecoded();
		assertTrue(readable.equals("s"));
		
		assertTrue(aQueue.hasNext() == false);
	}
	
	@Test
	public void fourTokensShortPause() {
		aQueue.addToken(DOT);
		aQueue.addToken(DASH);
		aQueue.addToken(DASH);
		aQueue.addToken(DOT);
		aQueue.addToken(SHORT_PAUSE);
		
		assertTrue(aQueue.hasNext() == true);
		
		String readable = aQueue.getNextDecoded();
		assertTrue(readable.equals("p"));
		
		assertTrue(aQueue.hasNext() == false);
	}

	@Test
	public void oneTokenLongPause() {
		aQueue.addToken(DOT);
		aQueue.addToken(LONG_PAUSE);
		
		assertTrue(aQueue.hasNext() == true);		
		String readable = aQueue.getNextDecoded();
		assertTrue(readable.equals("e"));
		
		assertTrue(aQueue.hasNext() == true);
		readable = aQueue.getNextDecoded();
		assertTrue(readable.equals(" "));
		
		assertTrue(aQueue.hasNext() == false);
	}
	
	@Test
	public void twoTokensLongPause() {
		aQueue.addToken(DASH);
		aQueue.addToken(DOT);
		aQueue.addToken(LONG_PAUSE);
		
		assertTrue(aQueue.hasNext() == true);
		
		String readable = aQueue.getNextDecoded();
		assertTrue(readable.equals("n"));
		
		assertTrue(aQueue.hasNext() == true);
		readable = aQueue.getNextDecoded();
		assertTrue(readable.equals(" "));
		
		assertTrue(aQueue.hasNext() == false);
	}
	
	@Test
	public void threeTokensLongPause() {
		aQueue.addToken(DOT);
		aQueue.addToken(DOT);
		aQueue.addToken(DOT);
		aQueue.addToken(LONG_PAUSE);
		
		assertTrue(aQueue.hasNext() == true);
		
		String readable = aQueue.getNextDecoded();
		assertTrue(readable.equals("s"));
		
		assertTrue(aQueue.hasNext() == true);
		readable = aQueue.getNextDecoded();
		assertTrue(readable.equals(" "));
		
		assertTrue(aQueue.hasNext() == false);
	}
	
	@Test
	public void fourTokensLongPause() {
		aQueue.addToken(DOT);
		aQueue.addToken(DASH);
		aQueue.addToken(DASH);
		aQueue.addToken(DOT);
		aQueue.addToken(LONG_PAUSE);
		
		assertTrue(aQueue.hasNext() == true);
		
		String readable = aQueue.getNextDecoded();
		assertTrue(readable.equals("p"));
		
		assertTrue(aQueue.hasNext() == true);
		readable = aQueue.getNextDecoded();
		assertTrue(readable.equals(" "));
		
		assertTrue(aQueue.hasNext() == false);
	}
	
	@Test
	public void badToken() {
		aQueue.addToken(DASH);
		aQueue.addToken(DASH);
		aQueue.addToken(DASH);
		aQueue.addToken(DASH);
		aQueue.addToken(LONG_PAUSE);
		
		assertTrue(aQueue.hasNext() == true);
		
		String readable = aQueue.getNextDecoded();
		assertTrue(readable.equals(MorseDecoder.BAD_MORSE));
	}
	
	@Test
	public void multipleLettersWithPauses() {
		aQueue.addToken(DOT);
		aQueue.addToken(DOT);
		aQueue.addToken(DOT);
		aQueue.addToken(SHORT_PAUSE);		
		assertTrue(aQueue.getNextDecoded().equals("s"));

		aQueue.addToken(DASH);
		aQueue.addToken(DASH);
		aQueue.addToken(DASH);
		aQueue.addToken(SHORT_PAUSE);
		assertTrue(aQueue.getNextDecoded().equals("o"));

		aQueue.addToken(DOT);
		aQueue.addToken(DOT);
		aQueue.addToken(DOT);
		aQueue.addToken(LONG_PAUSE);
		assertTrue(aQueue.getNextDecoded().equals("s"));
		assertTrue(aQueue.getNextDecoded().equals(" "));
		
		aQueue.addToken(DASH);
		aQueue.addToken(DASH);
		aQueue.addToken(DASH);
		aQueue.addToken(SHORT_PAUSE);
		assertTrue(aQueue.getNextDecoded().equals("o"));
		
		aQueue.addToken(DOT);
		aQueue.addToken(DOT);
		aQueue.addToken(DOT);
		aQueue.addToken(SHORT_PAUSE);
		assertTrue(aQueue.getNextDecoded().equals("s"));

		aQueue.addToken(DASH);
		aQueue.addToken(DASH);
		aQueue.addToken(DASH);
		aQueue.addToken(LONG_PAUSE);
		assertTrue(aQueue.getNextDecoded().equals("o"));
		assertTrue(aQueue.getNextDecoded().equals(" "));

		assertTrue(aQueue.hasNext() == false);
	}
}
